<?php
class GoodsModel extends ViewModel{
	public $table = 'goods';
	public $view = array(
			'category'=>array(
					'type'=>INNER_JOIN,
					'on'=>'goods.cid=category.cid'
				)
		);
	public $validate = array(
			array('gname','nonull','请输入商品名称',2,3),
			array('cid','nonull','请选择栏目',2,3)
		);
	public $auto=array(
		array('index_pic','indexPicAuto','method',2,3),
		array('list_pic','listPicAuto','method',2,3),
		array('addtime','strtotime','function',2,3),
		array('flag','flagstr','method',2,3)
	);
	public function flagstr($v){
		return !empty($v)?implode(",",$v):null;
	}
	public function indexPicAuto($v){
		//选了新的图片，并且还有老图片
		$old_pic = Q('in_pic');
		if(!empty($_FILES['index_pic']['name'])&&!empty($old_pic)){
			//上传新图
			$upload = new Upload();
			$info = $upload->upload('index_pic');
			//删掉老图
			unlink($old_pic);
			return $info[0]['path'];
		}else if(!empty($_FILES['index_pic']['name'])){
			//上传新图，没有老图
			$upload = new Upload();
			$info = $upload->upload('index_pic');
			return $info[0]['path'];
		}else if(!empty($old_pic)){
			//没有上传新图
			return $old_pic;
		}
	}
	public function listPicAuto($v){
		//选了新的图片，并且还有老图片
		$old_pic = Q('li_pic');
		if(!empty($_FILES['list_pic']['name'])&&!empty($old_pic)){
			//上传新图
			$upload = new Upload();
			$info = $upload->upload('list_pic');
			//删掉老图
			unlink($old_pic);
			return $info[0]['path'];
		}else if(!empty($_FILES['list_pic']['name'])){
			//上传新图，没有老图
			$upload = new Upload();
			$info = $upload->upload('list_pic');
			return $info[0]['path'];
		}else if(!empty($old_pic)){
			//没有上传新图
			return $old_pic;
		}
	}
	public function addGoods($data=array()){

		if($this->create()){
			if(empty($data)){
				$data =$this->data;//$this->data是经过自动执行自动验证后的$_POST数据，其中有index_pic数据
			}else{
				$data = array_merge($data,$this->data);
			}
			$gid = $this->add($data);

			if($gid){
				//插入图片
				$goodpicsModel = K('GoodsPic');
				$goodpicsModel->addPics($gid);
				//插入商品属性值
				$GoodsAttr = K('GoodsAttr');
				$GoodsAttr->addGoodsAttr($gid);
				//插入规格属性值
				
				
				//插入商品库存
				$StockModel = K('Stock');
				$StockModel->addStock($gid);
				return $gid;
			}else{
				$this->error = '商品添加失败';
				return false;
			}
		}
	}	


	public function saveGoods($data=array()){
		if($this->create()){
			$gid = Q('gid');
			if(empty($data)){
				$data =$this->data;//$this->data是经过自动执行自动验证后的$_POST数据，其中有index_pic数据
			}else{
				$data = array_merge($data,$this->data);
			}
			if($this->where(array('gid'=>$gid))->save($data)){
				//上传商品缩略图 
				$picsModel = K('GoodsPic');
				$picsModel->addPics($gid);

				//修改商品属性值表，1，删除商品原属性值。2，插入新属性值
				$goodsAttrModel = K('GoodsAttr');
				$goodsAttrModel->delGoodsAttr($gid);
				$goodsAttrModel->addGoodsAttr($gid);

				//更新库存表，不改变stid
				
				$StockModel = K('Stock');
				//删除库存表和库存属性表
				$StockModel->gidStockDel($gid);
				
				$StockModel->saveStock($gid);
				//修改规格：1，根据提交过来的stid隐藏域数组，删除库存表中的数据，删除库存属性关联表中数据
				//2,重新添加两个表中的新数据
				// $StockModel = K('Stock');

				// $stidArr = $_POST['stid'];//删除库存表，和库存属性表中数据
				// foreach ($stidArr as $key => $stid) {
				// 	$StockModel->delStock($stid);
				// }
				// //添加新数据
				// $StockModel->addStock($gid);

				return true;
			}else{
				$this->error = '商品更新失败';
				return false;
			}
		}else{
			$this->error = '商品更新时验证失败';
			return false;
		}
	}
	public function delGoods($gid){
		//删除商品表数据
		
		if($this->delete('gid='.$gid)){
			if(M('goods_attr')->delete('gid='.$gid)){
				return true;
			}else{
				$this->error .= '商品属性关联表删除失败';
				return false;
			}
		}else{
			$this->error = '商品表删除失败';
			return false;
		}
		//删除商品属性关联表数据
		
	}
}
?>
